package com.android.picotts.businiss.model;

import java.io.File;
import java.io.Serializable;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.android.picotts.R;
import com.android.picotts.ui.widget.SpirteView;
import com.android.picotts.util.IntentUtils;

/**
 * 精灵的基类
 * @author zhaohua
 */
public class Spriter implements Serializable{
    private static final long serialVersionUID = 939905642305336107L;
    
    /** 可显示的名称*/
    public String name;
    /** 可打开的uri*/
    public String uri;
    /** 类型*/
    public SpriterType type;
      
    public Spriter(SpriterType type, String name, String uri) {
        this.name = name;
        this.type = type;
        this.uri = uri;
    }
    
    public Spriter(File file) {
        this.name = file.getName();
        this.type = SpriterType.FILE;
        this.uri = file.getAbsolutePath();
    }
    
    @Override
    public boolean equals(Object o) {
        if(o instanceof Spriter) {
            Spriter one = (Spriter)o;
            return one.uri.equals(this.uri);
        }else {
            return super.equals(o);
        }
    }
    
    public String getDispalyName() {
        return name != null ? name : uri;
    }
    
    public int getLeftDrawable() {
        switch(type) {
            case FILE:{
                return R.drawable.privacy_tab_msg_selected;
            }
            case PHONE:{
                return R.drawable.privacy_tab_call_selected;
            }
            case SMSTO:{
                return R.drawable.privacy_tab_msg_selected;
            }
            case WEB:{
                return R.drawable.privacy_tab_msg_selected;
            }
            default:{
                return R.drawable.privacy_tab_msg_selected;
            }
        }
    }
    
    public void onClick(Context context) {
        Intent it = null;
        
        switch(type) {
            case APP:{
                it = IntentUtils.openApplacation(context, uri);
                break;
            }
            case FILE:{
                it = IntentUtils.openFile(uri, context);
                break;
            }
            case PHONE:{
                it = IntentUtils.dialPhone(uri);
                break;
            }
            case SMSTO:{
                it = IntentUtils.sendSms(uri, "");
                break;
            }
            case WEB:{
                it = IntentUtils.openLink(uri);
                break;
            }
        }
        if(it != null) {
            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                context.startActivity(it);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, R.string.application_not_available, Toast.LENGTH_SHORT).show();
            }
        }
    }   
    
    public View getView(LayoutInflater inflater, Spriter one, boolean isEditable) {
        View v = inflater.inflate(R.layout.view_spirte, null);
        v.setTag(one.uri);
        SpirteView view = (SpirteView)v;
        view.bindData(one, isEditable);
        return v;
    }
}